package com.nefu.editor.core.mapper.provider;

import com.nefu.editor.core.model.Article;
import com.nefu.editor.core.model.Vo.ArticleVo;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author chenchenT
 * @date : 18-11-13
 * @since : java 8
 */
public class ArticleProvider {

    public String selectArticleByCondition(Article article) {
        return new SQL() {
            {
                SELECT("article_systemId as systemId, article_title as title, article_writerId as writerId, article_thinkerId as thinkerId, " +
                        "article_wordCount as wordCount, article_time as time, article_image as image, article_summary as summary, article_keyWordId as keyWordId," +
                        "article_likesNumber as number, article_collectsNumber as collectsNumber,article_content as content");
                FROM("t_article");
                if (null != article.getSystemId()) {
                    WHERE("article_systemId=#{systemId}");
                }
                if (null != article.getWriterId()) {
                    WHERE("article_writerId=#{writerId}");
                }
            }
        }.toString();
    }

    public String selectLatestArticle(ArticleVo articleVo) {
        return new SQL() {
            {
                SELECT("article_systemId as systemId, article_title as title, article_writerId as writerId, article_thinkerId as thinkerId, " +
                        "article_wordCount as wordCount, article_time as time, article_image as image, article_summary as summary, article_keyWordId as keyWordId," +
                        "article_likesNumber as number, article_collectsNumber as collectsNumber,article_content as content");
                FROM("t_article");
                if (null != articleVo.getKeyWordId()) {
                    WHERE("article_keyWordId=#{keyWordId}");
                }
                if ((null != articleVo.getLatest() && articleVo.getLatest() == 1) || null == articleVo.getLatest()) {
                    ORDER_BY(" article_time desc limit " + articleVo.getL() + "," + articleVo.getOffset());
                }
                if (null != articleVo.getLatest() && 0 == articleVo.getLatest()) {
                    ORDER_BY(" article_likesNumber desc limit " + articleVo.getL() + "," + articleVo.getOffset());
                }
            }
        }.toString();
    }

}
